package cn.edu.xhu.library_management.user.controller;

import cn.edu.xhu.library_management.common.entity.R;
import cn.edu.xhu.library_management.user.entity.LmUserOhter;
import cn.edu.xhu.library_management.user.entity.search.OtherSearch;
import cn.edu.xhu.library_management.user.entity.search.TeacherSearch;
import cn.edu.xhu.library_management.user.service.LmUserOhterService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: MMJ
 * @Date: 2023 - 04 - 18
 * @Description: 校外用户控制类
 */
@Api(tags = "校外管理模块")
@ApiSort(3)
@RestController
@RequestMapping("/lm_user/user/other")
public class UserOtherController {

    @Autowired
    private LmUserOhterService otherService;
    @Autowired
    private BCryptPasswordEncoder encoder;
    @GetMapping("/search")
    @ApiOperation(value = "校外用户搜索", produces = "application/json")
    @ApiOperationSupport(order = 111)
    public R search(OtherSearch search){
        Page<LmUserOhter> others= otherService.search(search);
        return R.ok().data("page", others);
    }

    @GetMapping("/{othId}")
    @ApiOperation(value = "通过校外ID查询校外用户", produces = "application/json")
    @ApiOperationSupport(order = 112)
    public R getStudentById(@PathVariable("othId") String othId){
        LmUserOhter other = otherService.getById(othId);
        return R.ok().data("other", other);
    }

    @PostMapping
    @ApiOperation(value = "添加校外用户", produces = "application/json")
    @ApiOperationSupport(order = 121)
    public R addUserStudent(@RequestBody LmUserOhter userOther){
        String account = userOther.getOthAccount();
        String initPwd = account.substring(account.length() - 6);
        // 加密 默认密码 后六位
        userOther.setOthPwd(encoder.encode(initPwd));
        boolean flag = otherService.save(userOther);
        return flag ? R.ok("添加用户成功") : R.error("添加用户失败");
    }

    @PutMapping
    @ApiOperation(value = "修改校外用户", produces = "application/json")
    @ApiOperationSupport(order = 131)
    public R updateUserStudent(@RequestBody LmUserOhter userOther){
        boolean flag = otherService.updateById(userOther);
        return flag ? R.ok("修改用户成功") : R.error("修改用户失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过Id删除用户", produces = "application/json")
    @ApiOperationSupport(order = 141)
    public R deleteUserStudent(@PathVariable("id") String id){
        boolean flag = otherService.removeById(id);
        return flag ? R.ok("删除用户成功") : R.error("删除用户失败");
    }

    @DeleteMapping
    @ApiOperation(value = "删除用户(othIds)", produces = "application/json")
    @ApiOperationSupport(order = 142)
    public R deleteUserStudent(@RequestParam("othIds") List<String> othIds){
        boolean flag = otherService.removeBatchByIds(othIds);
        return flag ? R.ok("批量删除用户成功") : R.error("批量删除用户失败");
    }
}
